//
//  ActivityRecordController.m
//  EasyToTesco2.0
//
//  Created by 杨森 on 16/10/4.
//  Copyright © 2016年 EasyToTesco. All rights reserved.
//

#import "ActivityRecordController.h"
#import "ActRecordCell.h"

@interface ActivityRecordController ()<UITableViewDelegate,UITableViewDataSource>
{
    MBProgressHUD *progressHUD;
    //区分消费和积分
    BOOL _judgeRecord;
    NSUInteger _refreshIndex;
     UIImageView *noDrawImage;
}

@property (nonatomic,strong)NSArray *actRecordArr;

@property(nonatomic,strong)UITableView *actRecordView;

@property(nonatomic,strong)UILabel *totalLabel;

@property(nonatomic,strong)UIButton *selectBtn;

@property(nonatomic,strong)NSMutableArray *recordListArray;

@end


static NSString *const ActivityRecordreuseIdentifier = @"ActivityRecordreuseIdentifier";

@implementation ActivityRecordController


- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [self.navigationController setNavigationBarHidden:NO animated:YES];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.edgesForExtendedLayout = UIRectEdgeNone;
    [self buildView];
    [self addPullUpdateData];
    
    //默认消费
    _judgeRecord = YES;
    _refreshIndex = 1;
    [self getActivityRecordListData:@{@"username":USERNAME,@"type":@"1",@"p":@"1"}];
}

#pragma mark 购物消费积分点击事件
- (void)recordButtonDidClick:(UIButton *)sender
{
    _selectBtn.enabled = true;
    sender.enabled = false;
    _selectBtn = sender;
    
    [self.recordListArray removeAllObjects];
    
    if (sender.tag == 1010) {
        _judgeRecord = YES;
        [self getActivityRecordListData:@{@"username":USERNAME,@"type":@"1",@"p":@"1"}];
    }else{
        _judgeRecord = false;
        [self getActivityRecordListData:@{@"username":USERNAME,@"type":@"2",@"p":@"1"}];
    }
}


#pragma mark >>>>>>>>刷新
- (void)addPullUpdateData
{
    self.actRecordView.mj_footer = [TopPullRefreshAnaimation footerWithRefreshingTarget:self refreshingAction:@selector(getActivityRecordLoadMoreData)];
    
}

- (void)getActivityRecordLoadMoreData
{
    _refreshIndex ++;
    NSString *indexStr = [NSString stringWithFormat:@"%zd",_refreshIndex];
    
    if (_judgeRecord) {
        [self getActivityRecordListData:@{@"username":USERNAME,@"type":@"1",@"p":indexStr}];
    }else{
        [self getActivityRecordListData:@{@"username":USERNAME,@"type":@"2",@"p":indexStr}];
    }
    
    [self.actRecordView.mj_footer endRefreshing];
}

- (void)getActivityRecordListData:(NSDictionary *)params
{
    progressHUD = [MBProgressHUD loadingInformationToView];
    [self.view addSubview:progressHUD];
    
    [noDrawImage removeFromSuperview];
    
    [[NetWorkSharedTools sharedNetWorkTools] objectWithURLDict:params AndMenthodName:@"/api/Order/Trade" completeBlock:^(id object) {
       
        NSLog(@"%@",object);
        if (JudgeBackDataCode) {
            NSArray *arr = [object objectForKey:@"data"];
            for (NSDictionary *dict in arr) {
                [self.recordListArray addObject:dict];
                self.totalLabel.text = [NSString stringWithFormat:@"总额 : %@",[object objectForKey:@"sum"]];
            }
            
        }else{
            
            if ([params[@"p"] isEqualToString:@"1"]) {
                noDrawImage = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"noRecharge"]];
                [self.view addSubview:noDrawImage];
                
                [noDrawImage mas_makeConstraints:^(MASConstraintMaker *make) {
                    make.centerX.equalTo(self.view.mas_centerX);
                    make.centerY.equalTo(self.view.mas_centerY).offset(-HEIGHT/8);
                    make.width.mas_equalTo(WIDTH/2);
                    make.height.mas_equalTo(HEIGHT/4);
                }];
                
                [_actRecordView removeFromSuperview];
                self.view.backgroundColor = BACKGROUND_COLOR;
            }else{
                //没有更多数据
                [self.actRecordView.mj_footer endRefreshingWithNoMoreData];
            }
            
            
        }
        [progressHUD hide:YES];
        [self.actRecordView reloadData];

        
    }];
}

#pragma mark - Table view data source
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 1;
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return self.recordListArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    ActRecordCell *cell = [tableView dequeueReusableCellWithIdentifier:ActivityRecordreuseIdentifier];
    //12
    if (!cell) {
        cell = [[ActRecordCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:ActivityRecordreuseIdentifier];
        
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
    }
    
    if (_judgeRecord) {
         cell.titleLbl.text = @"赠送积分权";
    }else{
         cell.titleLbl.text = @"赠送代金券";
    }
   
    NSDictionary *dataDict = self.recordListArray[indexPath.section];
    
    if (_judgeRecord) {
         cell.numLbl.text = [NSString stringWithFormat:@"赠送%@",dataDict[@"give_money"]];
    }else{
        cell.numLbl.text = [NSString stringWithFormat:@"增送%@",dataDict[@"give_integral"]];
    }
    
   
    cell.timeLbl.text = [NSString stringWithFormat:@"%@",dataDict[@"create_time"]];

    return cell;
}


- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 3;
}


#pragma mark buildView
- (void)buildView
{
    for (int i = 0; i < 2; ++i) {
        
        UIButton *btn = [[UIButton alloc] init];
        [btn setTitle:[NSString stringWithFormat:@"%@",self.actRecordArr[i]] forState:UIControlStateNormal];
        btn.titleLabel.font = FONT(15);
        btn.titleLabel.font = [UIFont boldSystemFontOfSize:15];
        [btn setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
        [btn setTitleColor:DEFAULT_COLOR forState:UIControlStateDisabled];
        btn.frame = CGRectMake((WIDTH/2+0.5)*i,0,WIDTH/2-0.5,50);
        [self.view addSubview:btn];
        [btn setBackgroundColor:[UIColor whiteColor]];
        [btn addTarget:self action:@selector(recordButtonDidClick:) forControlEvents:UIControlEventTouchUpInside];
        btn.tag = 1010+i;
        if (btn.tag == 1010) {
            btn.enabled = false;
            _selectBtn = btn;
        }
    }
    //黑线
    UIView *lineView = [[UIView alloc] initWithFrame:CGRectMake(WIDTH/2-0.5,0,1,50)];
    lineView.backgroundColor = BACKGROUND_COLOR;
    [self.view addSubview:lineView];
    
    
    
    self.actRecordView = [[UITableView alloc] initWithFrame:CGRectMake(0,50,WIDTH,HEIGHT-164) style:UITableViewStylePlain];
    [self.view addSubview:self.actRecordView];
    _actRecordView.backgroundColor = BACKGROUND_COLOR;
    _actRecordView.delegate =self;
    _actRecordView.dataSource =self;
    _actRecordView.rowHeight = 50;
    _actRecordView.tableFooterView = [[UIView alloc] init];
    
    UIView *lineView1 = [[UIView alloc] initWithFrame:CGRectMake(0,HEIGHT-50-65,WIDTH,1)];
    lineView1.backgroundColor = BACKGROUND_COLOR;
    [self.view addSubview:lineView1];
    //总额
    UILabel *totalLabel = [[UILabel alloc] initWithFrame:CGRectMake(0,HEIGHT-50-64,WIDTH,50)];
    self.totalLabel = totalLabel;
    [self.view addSubview:totalLabel];
    totalLabel.textAlignment = NSTextAlignmentCenter;
    totalLabel.font = FONT(15);
    totalLabel.font = [UIFont boldSystemFontOfSize:15];
    totalLabel.textColor = DEFAULT_COLOR;
    totalLabel.backgroundColor = [UIColor whiteColor];
    
}


- (NSArray *)actRecordArr {
    if (!_actRecordArr) {
        _actRecordArr = @[@"购物积分权",@"购物代金券"];
    }
    return _actRecordArr;
}

- (NSMutableArray *)recordListArray
{
    if (!_recordListArray) {
        _recordListArray = [[NSMutableArray alloc] init];
    }
    return _recordListArray;
}

@end
